package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.R;
import com.itheima.entity.ShoppingCart;
import com.itheima.service.ShoppingCartService;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    ShoppingCartService shoppingCartService;

    @Autowired
    UserService userService;

    //查询购物车
    @GetMapping("/list")
    public R<List<ShoppingCart>> selectShopping(HttpServletRequest request) {
        Object user = request.getSession().getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> lqw1 = new LambdaQueryWrapper<>();
        lqw1.eq(ShoppingCart::getUserId, user);
        return R.success(shoppingCartService.list(lqw1));
    }

    //添加购物车
    @PostMapping("/add")
    public R<String> saveShopping(HttpServletRequest request, @RequestBody ShoppingCart sc) {
        Object user = request.getSession().getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, user);
        //判断添加的是菜品还是套餐
        if (sc.getDishId() != null) {
            lqw.eq(ShoppingCart::getDishId, sc.getDishId());
        } else {
            lqw.eq(ShoppingCart::getSetmealId, sc.getSetmealId());
        }
        ShoppingCart one = shoppingCartService.getOne(lqw);
        if (one != null) {
            one.setNumber(one.getNumber() + 1);
            shoppingCartService.updateById(one);
        } else {
            sc.setUserId((Long) user);
            sc.setNumber(1);
            shoppingCartService.save(sc);
        }
        return R.success("添加成功");
    }

    //清空购物车
    @DeleteMapping("/clean")
    public R<String> deleteShopping(HttpServletRequest request) {
        Object user = request.getSession().getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, user);
        shoppingCartService.remove(lqw);
        return R.success("删除成功");
    }

    //http://localhost:8080/shoppingCart/sub
    //购物车商品删减
    @PostMapping("/sub")
    public R<String> updateShopping(HttpServletRequest request, @RequestBody ShoppingCart sc) {
        Object user = request.getSession().getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, user);
        ShoppingCart one = shoppingCartService.getOne(lqw);
        if (one.getDishId() == null) {
            lqw.eq(ShoppingCart::getSetmealId, sc.getSetmealId());
            ShoppingCart one1 = shoppingCartService.getOne(lqw);
            if (one1.getNumber() > 0) {
                one1.setNumber(one1.getNumber() - 1);
                shoppingCartService.updateById(one1);
            }
            if (one1.getNumber() == 0) {
                shoppingCartService.removeById(one1.getId());
            }
        } else {
            lqw.eq(ShoppingCart::getDishId, sc.getDishId());
            ShoppingCart one1 = shoppingCartService.getOne(lqw);
            if (one1.getNumber() > 0) {
                one1.setNumber(one1.getNumber() - 1);
                shoppingCartService.updateById(one1);
            }
            if (one1.getNumber() == 0) {
                shoppingCartService.removeById(one1.getId());
            }
        }
        return R.success("修改成功");
    }
}
